// xjn_duhuang/pages/writeoff/writeoff.js
const app = getApp()
const { share, linkGo, ajax, apiDomain, toast } = require('../../comm.js');
Page({

  /**
   * 页面的初始数据   二维码 链接 https://dev-bdl.joylinksgroup.com/api/qrcode
   * test    111111
   */
  data: {
    username:'',
    password:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;

    var username = wx.getStorageSync('username')
    var password = wx.getStorageSync('password')

    if(username && password){
      _this.setData({
        username:username,
        password:password
      })
      wx.showLoading({
        title: '正在登录...',
      })
      //var url="/api/hexiao/user/loginbyaccount"; // 八达岭
      var url="/he_xiao_user/login"; // 敦煌
      ajax({
        url:url ,
        data: {
          "account": username,
          "password":password
        },
        success: res => {
          console.log("登录结果" , res)
          wx.hideLoading()
           if(res.data.code==200){ // 八达岭1  敦煌200

            wx.navigateTo({
              url: '../writescan/writescan?user_token='+res.data.data.token
            })
           }else{
             setTimeout(function(){
              wx.showToast({
                title: res.data.message,
                icon: 'none',
                duration: 3000
              })
            },250)
            
           } 
         
        }
      })
    }
  },
  usernameInput(e){
    this.setData({
      username:e.detail.value
    })
  },
  passwordInput(e){
    this.setData({
      password:e.detail.value
    })
  },
  loginScan(){
    console.log(111)
    var _this = this

    wx.showLoading({
      title: '正在登录...',
    })
    //var url="/api/hexiao/user/loginbyaccount"; // 八达岭
    var url="/he_xiao_user/login"; // 敦煌
    ajax({
      url:url ,
      data: {
        "account": _this.data.username,
        "password":_this.data.password
      },
      success: res => {
        console.log("登录结果" , res)
        wx.hideLoading()
         if(res.data.code==200){ // 八达岭1  敦煌200

          wx.setStorageSync('username', _this.data.username)
          wx.setStorageSync('password', _this.data.password)

          wx.navigateTo({
            url: '../writescan/writescan?user_token='+res.data.data.token
          })
         }else{
           setTimeout(function(){
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              duration: 3000
            })
          },250)
          
         } 
       
      }
    })
    
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return share;
  }
})